<?php
/** Clasa pentru configuratia generala a aplicatiei;
 * @package AMFramework
 * @subpackage Data_Pack
 * @author Marcu Alexandru
 * @version 1.0 $Build 305b  
 */
class Amfw_Database_Mysql extends Amfw_Database 
{
	// Protected ==============================================================
	protected function _initDbConnection() {
		// Initializarea conexiunii
		$link = mysql_connect($this->_config['host'], $this->_config['user'], $this->_config['password']);
		if(!$link) {
			$msg = sprintf("<b>Could not connect to Mysql:</b> %s",mysql_error());
			Amfw_Debug::toJournal($msg,D_ERROR,1);
		} else {
			$this->_linkId = $link;
		}
		// Daca nu este gasita baza de date se creeaza o eroare
		if(!mysql_select_db($this->_config['name'], $this->_linkId)) {
			$msg = sprintf("<b>Could not find the database named: %s.</b> Make sure that your database exists 
			and the name is correctly typed in ApplicationSetup.",$this->_config['name']);
			Amfw_Debug::toJournal($msg,D_ERROR,1);
		}
	}

	// Public =================================================================
	public function init() {
		// Initializarea obiectului folosit pentru lucrul cu baza de date
		// Se cheama in Amfw_Factory::_createDb()
		$this->__initMethods('Config','DbConnection');

		// Fiecare "copil" al Amfw_Database isi va defini 
		// independent rutina de initializare
	}

	public function query() {
		// Incrementam counterul pentru cereri
		Amfw_Debug::incQueryCount();

		// Salvam intr-o zona auxiliara rezultatul queryului
		$result = mysql_query($this->_query, $this->_linkId);

		// Pentru a il putea analiza si semnaliza erori
		if(!($result)) {
			$err = sprintf('<b>MySql Error on Query:</b> %d <br/><b>Error %s:</b> %s',
				$this->_query,mysql_errno($this->_linkId),mysql_error($this->_linkId));
			Amfw_Debug::toJournal($err,D_NOTE,0);
			$this->_cursor = null;
		} 
		$this->_cursor = $result;
	}
	
	public function escape($input) {
		// Intoarce textul sanitizat cu functia specifica BD
		return mysql_real_escape_string($input,$this->_linkId);
	}
	
	public function getRow() {
		$ret = null; 
		if(($row = mysql_fetch_row($this->_cursor)) !== false) {
			$ret = $row;
		}
		return $ret;
	}
	
	public function getRowList($key = null) {
		$array = array(); 
		while(($row = mysql_fetch_row($this->_cursor)) !== false) {
			if($key !== null) {
				$array[$row[$key]] = $row;
			} else {
				$array[] = $row;
			}
		}
		return $array;
	}

	public function getAssoc() {
		$ret = null;
		if(($row = mysql_fetch_assoc($this->_cursor)) !== false) {
			$ret = $row;
		}
		return $ret;
	}
	
	public function getAssocList($key = null) {
		$array = array(); 
		while(($row = mysql_fetch_assoc($this->_cursor)) !== false) {
			if($key !== null) {
				$array[$row[$key]] = $row;
			} else {
				$array[] = $row;
			}
		}
		return $array;	
	}

	public function getResult() {
        $ret = null;
        if (($row = mysql_fetch_row($this->_cursor)) !== false) {
            $ret = $row[0];
        }
        return $ret;
	}

	public function getResultList($col = 0) {
		$mysql_fetch = "mysql_fetch_";
		$mysql_fetch .= is_string($col) ? "assoc" : "row";
		 
		$array = array();
		while(($row = $mysql_fetch($this->_cursor)) !== false) {
			$array[] = $row[$col];
		}
		return $array;
	}
	
	public function getObject() {
        $ret = null;
		if(($object = mysql_fetch_object($this->_cursor)) !== false) {
			$ret = $object;
		}
		return $ret;
	}

	public function getObjectList($key = null) {
		$array = array();
		while(($row = mysql_fetch_object($this->_cursor)) !== false) {
            if ($key) {
                $array[$row->$key] = $row;
            } else {
                $array[] = $row;
            }
        }
        return $array;
	}
	public function getNumRows() {
		return mysql_num_rows($this->_cursor);
	}
	
	public function getCursor() {
		return $this->_cursor;
	}
	
	public function freeCursor() {
		mysql_free_result($this->_cursor);
	}
	
	function __destruct() {
		mysql_close($this->_linkId);
	}
}